<?php

namespace App\Http\Controllers\Company;
use App\Models\Api\ShieldCompany;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\CompanyJob;
use App\Models\XS\ResumeXS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style,Auth;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

class IntelligentController extends CommonController{
    /**
     * 智能按职位搜索推荐
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function search(Request $request){
        $pageInput=$request->only('id');
        $user = auth()->guard('company')->user();
        $companyJob=CompanyJob::where("uid",$user->uid)->where("id",$request->id)->first();
        if(empty($companyJob->id)){
            abort(404);
        }
        //处理数据查询
        if (\Site::get('xsOpen')) {
            $resumeinfo = $this->xunSearch($request,$companyJob);
            if ($resumeinfo->total() < 1); $resumeinfo = $this->sqlSearch($request,$companyJob);
        } else {
            $resumeinfo = $this->sqlSearch($request,$companyJob);
        }

        foreach($resumeinfo as $k => $value) {
            $value->name = msubstr($value->name, 0, 8);
            //性别
            switch ($value->sex) {
                case 0:
                    $value->sex = '';
                    break;
                case 1:
                    $value->sex = '男';
                    break;
                case 2:
                    $value->sex = '女';
                    break;
            }
            //年龄
            if (empty($value->birthday)) {
                $value->birthday = '';
            } else {
                $value->birthday = birthday($value->birthday);    //年龄
            }
            //现工作地
            if (empty($value->homeaddress)) {
                $value->homeaddress = '';
            } else {
                $value->homeaddress = CollArea::getAreaName($value->homeaddress);
            }
            //学历
            if (empty($value->education)) {
                $value->education = '';
            } else {
                $value->education = CollOption::getOptionName($value->education, 'education');
            }
            //工作经验
            if (empty($value->work_year)) {
                $value->work_year = '';
            } else {
                $value->work_year = CollOption::getOptionName($value->work_year, 'work_year');
            }
            //工作地区
            if (empty($value->jobarea1)) {
                $value->jobarea1 = '';
            } else {
                $value->jobarea1 = CollArea::getAreaName($value->jobarea1);
            }
            if (empty($value->jobarea2)) {
                $value->jobarea2 = '';
            } else {
                $value->jobarea2 = CollArea::getAreaName($value->jobarea2);
            }
            if (empty($value->jobarea3)) {
                $value->jobarea3 = '';
            } else {
                $value->jobarea3 = CollArea::getAreaName($value->jobarea3);
            }
            if (empty($value->jobarea4)) {
                $value->jobarea4 = '';
            } else {
                $value->jobarea4 = CollArea::getAreaName($value->jobarea4);
            }
            if (empty($value->jobarea5)) {
                $value->jobarea5 = '';
            } else {
                $value->jobarea5 = CollArea::getAreaName($value->jobarea5);
            }
            //更新时间
            $value->updatetime=timeTranx($value->updatetime);
        };

        return Style::view('company.intelligentSearch',compact('resumeinfo','companyJob','pageInput'));
    }

    /** 迅搜搜索
     * @param $request
     * @param $companyJob
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function xunSearch($request,$companyJob)
    {
        //查询条件
        $q = [];

        //黑名单id
        $user = Auth::guard('company');
        if($user->check()) {
            $shield = ShieldCompany::where('company_uid', $user->user()->uid)->pluck('resume_id');
            if (count($shield) > 0) {
                $q['shield'] = $shield->toArray();
            }
        }

        $jobarea=[];//工作地点
        $jobsort=[];//所属岗位
        for ($i=1;$i<=5;$i++) {
            $area=$companyJob->{'location'.$i};
            $sort=$companyJob->{'job_category'.$i};
            if (!empty($area)) {
                $jobarea[]=$area;
            }
            if (!empty($sort)) {
                $jobsort[]=$sort;
            }
        }
        $q['jobarea']=$jobarea;
        $q['jobsort']=$jobsort;
        $q['by_day'] = true;

        //关键字
        $q['keyword'] = $companyJob->job_title;

        //创建迅搜对象
        $xs = new ResumeXS();
        //查询页码
        $page = $request->input('page', 1);
        //搜索(参数:搜索条件,第几页,一页个数,获取总数,按照时间排序)
        $arr = $xs->searchResume($q, $page,20,true,true);

        //分页并返回
        return $this->pageInfo($arr['data'],$arr['total'],$request);
    }

    /** 数据库搜索
     * @param $request
     * @param $companyJob
     * @return mixed
     */
    public function sqlSearch($request,$companyJob)
    {
        $resume = DB::table('resume as t1')
            //获取字段
            ->select(
                'id','uid','name','sex','birthday','education','homeaddress',
                'work_year','intentionjobs','jobarea1','jobarea2','jobarea3',
                'jobarea4','jobarea5','updatetime','avatar','resume_status'
            )
            ->where('resume_status',1)//公开的简历
            //工作地点
            ->where(function ($query) use ($companyJob) {
                for($i=1;$i<=5;$i++){
                    $location=$companyJob->{'location'.$i};
                    if(!empty($location)){
                        $query->orWhere('jobarea1', '=', $location)
                            ->orWhere('jobarea2', '=', $location)
                            ->orWhere('jobarea3', '=', $location)
                            ->orWhere('jobarea4', '=', $location)
                            ->orWhere('jobarea5', '=', $location);
                    }
                    else break;
                }
            })
            ->where(function ($query) use ($companyJob) {
                for($i=1;$i<=5;$i++){
                    $job_category=$companyJob->{'job_category'.$i};
                    if(!empty($job_category))
                        $query->orWhere('jobsort1', '=', $job_category)
                            ->orWhere('jobsort2', '=', $job_category)
                            ->orWhere('jobsort3', '=', $job_category)
                            ->orWhere('jobsort4', '=', $job_category)
                            ->orWhere('jobsort5', '=', $job_category);
                    else break;
                }
            });

        $count = $resume->count();

        $resumes=$resume->orderBy('t1.updatetime','desc')->orderBy('t1.id','desc')
            ->skip($this->getSkip($request->page))->take(20)->get();

        return $this->pageInfo($resumes,$count,$request);
    }


    /**
     * 分页信息
     */
    protected function pageInfo($data,$total,$request){
        $perPage = 20;//每页显示20条
        $current_page = $request->input('page',1);
        $total=$total>4000?4000:$total;
        $company_job = new LengthAwarePaginator($data, $total, $perPage, $current_page,[
            'path' => Paginator::resolveCurrentPath(), //注释2
            'pageName' => 'page',
        ]);
        return $company_job;
    }

    /**
     * 取得分页启始数
     */
    protected function getSkip($page){
        if(empty($page)) $page=1;
        if($page>200) $page=200;
        return ($page-1)*20;
    }
}